(clear)

;; pentru conectarea la baza de date mysql se foloseste un contribution la JESS
;; Fact Storage Provider Framework
;; http://www.jessrules.com/jesswiki/view?FactStorageProviderFramework

;;
;; Initialization
;;

;; in init.jess sunt definiti parametrii de conectare la baza de date

(batch database/init.jess)

;; fiecare tabela din baza de date se echivaleaza cu un template jess
;; iar fiecare linie din tabela se va incarca intr-un fact

(deftemplate tempstats-archiveConsumCurentCameraA
	(slot timp (type STRING))
	(slot consum_curent (type FLOAT)))

(deftemplate tempstats-archiveConsumCurentCameraB
	(slot timp (type STRING))
	(slot consum_curent (type FLOAT)))

(deftemplate tempstats-archiveConsumCurentCameraC
	(slot timp (type STRING))
	(slot consum_curent (type FLOAT)))

(deftemplate tempstats-archiveConsumCurentTotal
	(slot timp (type STRING))
	(slot consum_curent (type FLOAT)))

(deftemplate tempstats-archiveTemperatura
	(slot timp (type STRING))
	(slot temperatura (type FLOAT)))

;; conectarea la baza de date
(bind ?provider (get-provider provider.tempstats ?url ?user ?password))

;; urmatoarea functie, conform notelor de la FSPF, necesita refactorizarea frmawork-ului
;; deoarece nu functioneaza pe versiunile recente de JESS
;; si probabil va genera erori la rularea sub JESS 7

(provide ?provider FULL tempstats)


;; se parcurg tabelele linie cu linie
(fsm-do-backward-chaining tempstats.archiveConsumCurentCameraA)
(fsm-do-backward-chaining tempstats.archiveConsumCurentCameraB)
(fsm-do-backward-chaining tempstats.archiveConsumCurentCameraC)
(fsm-do-backward-chaining tempstats.archiveConsumCurentTotal)
(fsm-do-backward-chaining tempstats.archiveTemperatura)

;;Read from database

(printout t crlf "READING ..." crlf)
(reset)

;; select pe baza de date si inserarea rezultatului query-ului in facts

(expand (assert (fsm-select (need-tempstats-archiveConsumCurentCameraA))))  ; Read and assert all  records from CameraA
(expand (assert (fsm-select (need-tempstats-archiveConsumCurentCameraB))))  ; Read and assert all  records from CameraA
(expand (assert (fsm-select (need-tempstats-archiveConsumCurentCameraC))))  ; Read and assert all  records from CameraA
(expand (assert (fsm-select (need-tempstats-archiveConsumCurentTotal))))  ; Read and assert all  records from CameraA
(expand (assert (fsm-select (need-tempstats-archiveTemperatura))))  ; Read and assert all  records from Temperatura
;; tipareste facts
(facts)